package com.wzapp.sitposture.eshop.service.impl;

import com.wzapp.sitposture.common.constant.PayConst;
import com.wzapp.sitposture.common.error.CustomizedException;
import com.wzapp.sitposture.common.util.ToolUtil;
import com.wzapp.sitposture.eshop.entity.OrderPay;
import com.wzapp.sitposture.eshop.repository.OrderPayRepository;
import com.wzapp.sitposture.eshop.service.OrderPayService;
import com.wzapp.sitposture.eshop.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class OrderPayServcieImpl implements OrderPayService{

    private static final String LOCK = "lock";

    @Autowired
    OrderService orderService;

    @Autowired
    OrderPayRepository orderPayRepository;

    /**
     * 功能: 更新订单支付状态 <br/>
     *
     * @param orderCode
     * @param tradeCode
     * @param success
     * @return void
     * @author wangzhijun
     * @date 2018-01-31 18:36
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOrderPayed(String orderCode, String tradeCode, Integer payMethod, boolean success) throws CustomizedException {
        log.debug("=============updateOrderPayed======1" + ", " + orderCode + ", " + tradeCode + ", " + success);
        if (ToolUtil.isEmpty(orderCode)) {
            return;
        }

        synchronized (LOCK) {
            OrderPay orderPay = orderPayRepository.findByOrderCode(orderCode);
            processPayNotify(tradeCode, payMethod, success, orderPay);
        }
    }

    private void processPayNotify(String tradeCode, Integer payMethod, boolean success, OrderPay
            orderPay) throws CustomizedException {
        log.info("=============processPayNotify======start" + orderPay);
        if (ToolUtil.isEmpty(orderPay)) {
            throw new CustomizedException("eShop.order.pay.error");
        }

        if (PayConst.ORDER_PAY_SUCCESS.equals(orderPay.getStatus())) {
            return;
        }

        if (!success) {
            log.info("=============processPayNotify======1-start");
            orderPay.setStatus(PayConst.ORDER_PAY_FAIL);
            orderPayRepository.save(orderPay);
            log.info("=============processPayNotify======1-end");
        } else {
            log.info("=============processPayNotify======2-start");
            orderPay.setStatus(PayConst.ORDER_PAY_SUCCESS);
            orderPay.setPayCode(tradeCode);
            orderPay.setPayMethod(payMethod);
            orderPay.setReceiptsAmount(orderPay.getAmount());
            orderPay.setUpdateTime(System.currentTimeMillis());
            orderPayRepository.save(orderPay);

            orderService.processPayOrderSuccess(orderPay.getOrderId(), payMethod, tradeCode);

            log.info("=============processPayNotify======2-end");
        }

        log.info("=============processPayNotify======end");
    }
}
